package com.itboye.shop.category.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.itboye.shop.R;
import com.itboye.shop.base.BaseActivity;
import com.itboye.shop.bean.DingDanQuery;
import com.itboye.shop.net.ResultEntity;
import com.itboye.shop.util.helper.ImageLoader;
import com.itboye.shop.util.loadutil.Const;
import com.itboye.shop.util.loadutil.MAlert;

import java.util.List;
import java.util.Observable;
import java.util.Observer;


/**
 * 订单查询
 */
public class GoodsOrderAdapter extends BaseAdapter implements Observer {
    List<DingDanQuery.ListEntity> beanList;
    Activity activity;
//    OrderPresenter orderPresenter;
    int type;
    private String tempOrderCode;

    public GoodsOrderAdapter(List<DingDanQuery.ListEntity> beanList, Activity activity, int type){
        this.beanList=beanList;
        this.activity=activity;
        this.type=type;
//        orderPresenter=new OrderPresenter(this);
    }
    public void setStatus(int type) {
        this.type = type;
    }

    @Override
    public int getCount() {
        return beanList.size();
    }

    @Override
    public Object getItem(int position) {
        return beanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
       ViewHolder holder=null;
        if(view==null){
            holder=new ViewHolder();
            view= LayoutInflater.from(activity).inflate(R.layout.item_goodsorder,null);
            holder.order_geshu= (TextView) view.findViewById(R.id.order_geshu);
            holder.order_money= (TextView) view.findViewById(R.id.order_money);
            holder.order_right= (TextView) view.findViewById(R.id.order_right);
            holder.order_left= (TextView) view.findViewById(R.id.order_left);
            holder.order_storename=(TextView) view.findViewById(R.id.order_storename);
            holder.txt_order_status=(TextView) view.findViewById(R.id.txt_order_status);
            holder.parent=(LinearLayout) view.findViewById(R.id.parent);
            holder.store_image=(SimpleDraweeView) view.findViewById(R.id.store_image);
            holder.goods_container= (LinearLayout) view.findViewById(R.id.goods_container);
            holder.totalPriceAndNum=(TextView)view.findViewById(R.id.goods_totalnum);
            view.setTag(holder);
        }else{
            holder= (ViewHolder) view.getTag();
        }

        holder.order_storename.setText(beanList.get(position).getStoreName());
        holder.order_geshu.setText("订单号"+beanList.get(position).getOrder_code());
        ImageLoader.loadImage(holder.store_image,Const.IMAGE_HEAD+beanList.get(position).getStoreLogo());
    //    holder.order_title.setText(beanList.get(position).getItems().get(position).getName());
    //    holder.order_price.setText("￥"+beanList.get(position).getItems().get(position).getPrice());
     //   holder.order_num.setText("*"+beanList.get(position).getItems().get(position).getCount());

        List<DingDanQuery.ListEntity.ItemsEntity> item=beanList.get(position).getItems();
        String name="";
        String price="";
        String count="";
        String img="";
        for(int i=0;i<item.size();i++){
            View viewGoods= LayoutInflater.from(activity).inflate(R.layout.item_ordergoods,null);
            SimpleDraweeView goods_icon= (SimpleDraweeView) viewGoods.findViewById(R.id.goods_icon);
            TextView goods_name=(TextView) viewGoods.findViewById(R.id.goods_name);
            TextView price_new=(TextView) viewGoods.findViewById(R.id.price_new);
            TextView price_old=(TextView) viewGoods.findViewById(R.id.price_old);
            TextView goods_guige=(TextView) viewGoods.findViewById(R.id.goods_guige);
            TextView goods_biaoqian=(TextView) viewGoods.findViewById(R.id.goods_biaoqian);
            TextView txt_num=(TextView) viewGoods.findViewById(R.id.txt_num);
            ImageLoader.loadImage(goods_icon,Const.IMAGE_HEAD + item.get(i).getImg());
            goods_name.setText(item.get(i).getName());
            price_new.setText("￥"+Double.parseDouble(item.get(i).getPrice())/100);
            price_old.setText("￥"+Double.parseDouble(item.get(i).getPrice())/100);
            goods_guige.setText(item.get(i).getSku_desc());
            txt_num.setText("x"+item.get(i).getCount());
            holder.goods_container.addView(viewGoods);
        }
        //[0=>全部,1=>待付款,2=>待发货,3=>待收货,4=>已收货, 7=>已完成 8=>已关闭]
        int orderStatus=Integer.parseInt(beanList.get(position).getOrder_status());
        if(type==0) {
            setButton(holder, orderStatus);
        }else{
            setButton(holder,type);
        }
        holder.order_money.setText("￥"+ Double.parseDouble(beanList.get(position).getPrice())/100);
        //去支付
        holder.order_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView textView= (TextView) v;
//                if(textView.getText().equals(MyApplcation.ctx.getString(R.string.quzhifu))) {
//                    Intent intent = new Intent(activity, GoPayActivity.class);
//                    intent.putExtra("order_code", beanList.get(position).getOrder_code());
//                    intent.putExtra("type", "two");//用来标示是首次支付还是重新支付
//                    activity.startActivity(intent);
//                }else if(textView.getText().equals("确认收货")){
//                    String uid= SPUtils.get(activity,null,Const.ID,"")+"";
//                    String sid=SPUtils.get(MyApplcation.ctx,null, Const.S_ID,"")+"";
//                   orderPresenter.querenShouHuo(sid,uid,beanList.get(position).getOrder_code());
//                }else if(textView.getText().equals("查看物流")){
//                    String uid= SPUtils.get(activity,null,Const.ID,"")+"";
//                    String password=SPUtils.get(activity,null,Const.PASSWORD,"")+"";
//                    Intent intent = new Intent(activity, WebActivity.class);
//                    intent.putExtra("url", "http://api.ryzcgf.com/public/web.php/Express/index?order_code="+beanList.get(position).getOrder_code()+"&uid="+uid+"&psw="+password);
//                    intent.putExtra("title", "物流信息");//用来标示是首次支付还是重新支付
//                    activity.startActivity(intent);
//                }
            }
        });
        holder.order_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                String uid= SPUtils.get(activity,null,Const.ID,"")+"";
//                String sid=SPUtils.get(MyApplcation.ctx,null,Const.S_ID,"")+"";
//                tempOrderCode=beanList.get(position).getOrder_code();
//                orderPresenter.querenShouHuo(sid,uid,beanList.get(position).getOrder_code());
            }
        });
//        //点击标题跳转到详情
//        holder.order_title.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
////                Intent intent=new Intent(activity, OrderDetailActivity.class);
////                intent.putExtra("order_code",beanList.get(position).getOrder_code());
////                activity.startActivity(intent);
//            }
//        });
        holder.parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent=new Intent(activity, OrderDetailActivity.class);
//                intent.putExtra("order_code",beanList.get(position).getOrder_code());
//                activity.startActivity(intent);
            }
        });

        return view;
    }

    private void setButton(ViewHolder holder,int orderStatus) {
        String status="";
        switch (orderStatus){
            case 1:
                status="待付款";
                holder.order_left.setVisibility(View.GONE);
                holder.order_right.setVisibility(View.VISIBLE);
                holder.order_right.setText(R.string.quzhifu);
                break;
            case 2:
                status="待发货";
                holder.order_left.setVisibility(View.GONE);
                holder.order_right.setVisibility(View.GONE);
                break;
            case 3:
                status="待收货";
                holder.order_left.setVisibility(View.VISIBLE);
                holder.order_right.setVisibility(View.VISIBLE);
                holder.order_left.setText("确认收货");
                holder.order_right.setText("查看物流");
                break;
            case 4:
                status="已收货";
                break;
            case 5:
                status="待付款";
                break;
            case 6:
                status="待付款";
                break;
            case 7:
                status="已完成";
                holder.order_left.setVisibility(View.GONE);
                holder.order_right.setVisibility(View.GONE);
                holder.order_right.setText("查看详情");
                break;
            case 8:
                status="已关闭";
                holder.order_left.setVisibility(View.GONE);
                holder.order_right.setVisibility(View.GONE);
                break;
        }

        holder.txt_order_status.setText(status);
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity=handlerError(data);
//        if (entity!=null) {
//            if (entity.getEventType()== OrderPresenter.querenshouhuo_success) {
//                ByAlert.alert(entity.getData());
//                Iterator<DingDanQuery.ListEntity> iterator = beanList.iterator();
//                while (iterator.hasNext()){
//                    DingDanQuery.ListEntity listEntity=iterator.next();
//                    if (listEntity.getOrder_code().equals(tempOrderCode)) {
//                        iterator.remove();
//                        if(beanList.size()<=0){
//                            ByAlert.alert("没有相关订单了哦");
//                        }
//                        notifyDataSetChanged();
//                        break;
//                    }
//                }
//            }else if (entity.getEventType()== OrderPresenter.querenshouhuo_fail) {
//                ByAlert.alert(entity.getData());
//            }
//        }
    }
    public static final String EVENT_TYPE_UNKNOWN = BaseActivity.class
            .getName() + "_unknown";

    /**
     * @param data
     * @return 2016-4-23 hebidu
     */
    public ResultEntity handlerError(Object data) {
        ResultEntity result = (ResultEntity) data;
        if (result == null) {
            result = new ResultEntity("-1", "数据格式错误!~", data);
            return result;
        }

        if (result.hasError()) {
            MAlert.alert(result.getMsg());
            result.setEventType(EVENT_TYPE_UNKNOWN);
            return result;
        }

        return result;
    }



    class ViewHolder{
        SimpleDraweeView store_image;
        TextView order_geshu,order_money,order_right,order_left,order_storename,txt_order_status,totalPriceAndNum;
        LinearLayout parent,goods_container;
    }
}
